<html>

	<head>
		<meta charset="utf-8">
		<title>websocket</title>
	</head>


	<script type="text/javascript">
		
		function loopSend(){


			var content			= postC();
			var time			= timeInput("send_input");
			var postTotalSize	= 0;
			var postIndex		= 0;
			var ws				= new WebSocket("ws://127.0.0.1:5567");




			// open 
			ws.onopen = function(){

				console.log("connect [ws://127.0.0.1:5567] success.");

				// 开始时间
				var beginTime = (new Date()).valueOf();


				var timeIntVal = setInterval(function(){
					var postSize = content.length;
					postTotalSize = postTotalSize + postSize;
					postIndex++;

					var postDetail = "time:["+ GetDate(2) + "]. post size:["+bytesToSize(postSize)+"]. post total size:["+bytesToSize(postTotalSize)+"]. post index:["+postIndex+"]";
					ws.send(postDetail);
					console.log(postDetail);



					if(postIndex >= 100){
						clearInterval(timeIntVal);
						// 开始时间
						var endTime = (new Date()).valueOf();
						console.log("====数据发送结束====");
						console.log("共发送：【"+postIndex+"】. 数据流量：【"+bytesToSize(postTotalSize)+"】. 耗时【"+showTime(beginTime,endTime)+"】");
						ws.close();
					}

				},time);

			};

			// message
			ws.onmessage = function (evt) { 
				var received_msg = evt.data;
				console.log("received_msg -- " + received_msg);
			};


			// close
			ws.onclose = function(){ 
				console.log("close");
			};

		}



		function loopConnect(){

			var content = "其实两个东西完全可以相互模拟，具体使用那个，看当时的需属要而定了。";

			var timeStr = document.getElementById("connect_input").value;
			if("" === timeStr){
				console.log("loopConnect time is empty");
				return;
			}
			var time = parseInt(timeStr);

			setInterval(function(){
				// 打开一个 web socket
				var ws = new WebSocket("ws://127.0.0.1:5567");
				// open 
				ws.onopen = function(){
					console.log("connect [ws://127.0.0.1:5567] success.");
					ws.send("时间: "+ GetDate(2) + "。内容: " + content);
					console.log("close [ws://127.0.0.1:5567] success.");
				};


				// message
				ws.onmessage = function (evt) { 
					var received_msg = evt.data;
					console.log("received_msg: "+ received_msg);
					ws.close();
				};

			},time);
		}



         // 获取时间
		function GetDate(format) {
			/**
			* format=1表示获取年月日
			* format=0表示获取年月日时分秒
			* **/
			var _time = "";
			var now = new Date();
			var year = now.getFullYear();
			var month = now.getMonth()+1;
			var date = now.getDate();
			var day = now.getDay();//得到周几
			var hour = now.getHours();//得到小时
			var minu = now.getMinutes();//得到分钟
			var sec = now.getSeconds();//得到秒
			if (format==1){
				_time = year+"-"+month+"-"+date;
			}else if (format==2){
				_time = year+"-"+month+"-"+date+" "+hour+":"+minu+":"+sec;
			}
			return _time;
		}




		//文件大小转换
		function bytesToSize(bytes) {
			if (bytes === 0) return '0 B';
			var k     = 1024;
			var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
			var i     = Math.floor(Math.log(bytes) / Math.log(k));
			return (bytes / Math.pow(k, i)).toPrecision(4) + ' ' + sizes[i];
		} 



		// 获取 发送内容，指定长度
		function postC() {
			var postC		= "";
			var postSize	= 1024 * 10;
			for (var i = postSize; i >= 0; i--) {
				postC = postC + "w";
			}
			return postC;
		}


		// send_input
		function timeInput(input){
			var timeStr = document.getElementById(input).value;
			if("" == timeStr){
				console.log("loopSend time is empty");
				return;
			}
			var time = parseInt(timeStr);
			return time;
		}



		function showTime(beginTime,endTime){


			var t = endTime - beginTime;
			if(t <= 0){
				return "错误时间";
			}

			var min = 0;
			var sec = 0;
			var ms = 0;

 
			var newTime  = t / 1000 + "";
			var tmp = newTime.split(".");
			sec = parseInt(tmp[0]);
			ms = parseInt(tmp[1]);

			if(sec <= 60){
				return sec + "秒" + ms + "毫秒";
			}

			min = sec / 60;
			sec = sec % 60;
			return min + "分 "+ sec + "秒" + "0." + ms + "毫秒";;
		}


	</script>


	<body>
		
		<div style="line-height: 30px;height: 30px;">
			<label>间隔时间：</label><input type="text" name="send" id="send_input" />MS
			<button type="button" onclick="loopSend()">loop send</button>
		</div>


		<div style="line-height: 30px;height: 30px;">
			<label>间隔时间：</label><input type="text" name="connect" id="connect_input" />MS
			<button type="button" id="connect" onclick="loopConnect()">loop connect</button>
		</div>



	</body>
		


</html>